package com.wude.guild.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mc.developmentkit.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.wude.bean.ShareInfo;
import com.wude.guild.LoginActivity;
import com.wude.guild.R;
import com.wude.guild.dialog.SharedDialog;
import com.wude.guild.servercore.mints.TitleBaseFragment;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;
import com.wude.tools.Utils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by 唐小妹 on 2017/11/29 0029.
 * 运营商活动分享
 */
public class SharedFragment extends TitleBaseFragment implements View.OnClickListener {
    private TextView tv_shared;
    private String tag;

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.shared_fragment, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        setHeaderTitle("分享");

        tv_shared = findView(view, R.id.tv_shared);
        tv_shared.setOnClickListener(this);
    }


    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("SharedFragment");
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("SharedFragment");
    }

    @Override
    public void onClick(View view) {
        if (Utils.getLoginUser() != null) {
            //获取分享信息
            Map<String, String> dataMap = new HashMap<>();
            dataMap.put("operator", tag);
            dataMap.put("token", Utils.getLoginUser().token);
            HttpCom.POST(khandler, HttpCom.getActivityShare(), dataMap, false);
        } else {
            startActivity(new Intent(getActivity(), LoginActivity.class));
        }
    }

    @Override
    public void onEnter(Object data) {
        super.onEnter(data);
        tag = (String) data;
    }

    /**
     * 获取分享内容
     */
    private SharedDialog dialog;
    @SuppressLint("HandlerLeak")
    Handler khandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    ShareInfo bena = HttpUtils.DNSShare(msg.obj.toString());
                    if (bena != null) {
                        if (dialog == null) {
                            dialog = new SharedDialog(getContext(), bena, 2);
                            dialog.show();
                        } else if (dialog != null && !dialog.isShowing()) {
                            dialog.show();
                        }
                    } else {
                        ToastUtil.showToast("分享失败");
                    }
                    break;
                case 2:

                    break;
                default:
                    break;
            }
        }
    };
}
